package com.jixi.handler;

import cn.dev33.satoken.exception.NotLoginException;
import com.jixi.common.constant.ResultCodeConstant;
import com.jixi.common.exception.BaseException;
import com.jixi.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        log.error(e.getMessage());
        return Result.fail(ResultCodeConstant.FAIL, e.getMessage());
    }

    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public Result error(BaseException e) {
        log.error(e.getMessage());
        return Result.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    @ResponseBody
    public Result error(NotLoginException e) {
        log.error(e.getMessage());
        return Result.fail(ResultCodeConstant.NOT_LOGIN, e.getMessage());
    }
}
